<?php
class MiactivityChannelAction extends Action{
	public function _initialize(){

		$this->retVal = Session::init();
		if (!$this->retVal) {
			Output::instance()->resultCode(code_access_denied);
			exit;
		}
		$cid = Session::$user["customerId"];
		Session::switchCustomer(0);
		$this->miactivity_channel_db = D('MiactivityChannel');
		$this->cid = $cid;
		Session::switchCustomer($cid);
	}
	public function update(){
		$result = array();
		$map = array();
		$map['cid'] = $this->cid;
		$map['name'] = $_POST['name'];
		$map['id'] = array('neq',$_POST['id']);

		$count = $this->miactivity_channel_db->where($map)->count();
		if($count > 0){
            $result['code'] = 0;
            $result['info'] = '';
		}else{
			$_POST['cid'] = $this->cid;
			$result_update = $this->miactivity_channel_db->update();
			
			if ($result_update) {
				$result['code'] = 1;
				$result['id'] = $result_update;
			} else {
				$result['code'] = 0;
				$error = $this->miactivity_channel_db->getError();
				$result['info'] = $error?$error:"发生未知错误";
			}
		}
		
		Output::instance()->object($result);
	}
	public function getChannelDetail($id){

	}
	/*
	*获取渠道列表
	*/
	public function getChannelList($order = 'id asc',$start=0,$limit=10,$returnJson=true){
		
		$map = array();
		$map['cid'] = $this->cid;
		$map['deleted'] = 0;
		if($returnJson==true){
			$list = $this->miactivity_channel_db
			->where($map)
			->order($order)
			->limit("$start, $limit")
			->select();
		}else{
			$list = $this->miactivity_channel_db
			->where($map)
			->order($order)
			->select();

		}
		

		if($list) {
			if($returnJson==true){
			$count =$this->miactivity_channel_db        		
			->where($map)        			
			->count();
			Output::instance()->objArray($list,$count);
		  }else{
		  	return $list;
		  }
		} else {
			Output::instance()->resultCode();
		}

	}
	public function exportChannel($base_url){
		$result = array();
        $expTitle = '渠道列表' ; //设置文件名
        $fileName = '渠道列表' . date('Ymd'); //设置文件名
        $list = $this->getChannelList('id asc',0,0,false);
        foreach ($list as $key => $value) {
        	$base_url_new = str_replace('replace_channel_id',$value['id'], $base_url);
        	$value['link'] = $base_url_new ;
        	$list[$key] = $value;
        }
        $expCellName  = array(
            array('name','渠道名称'),
            array('link','推广链接'),
           
        );
        $expTableData = $list;
        
              
        $expTableDataTemp = array();
        foreach ($expTableData as $key => $value) {
            $expTableDataTemp[] = $value;
        }
        // var_dump($expTableData);
        // exit;
        if($expTableDataTemp){
            Output::instance()->objArrayExcel($fileName,$expCellName,$expTableDataTemp);
        }
	}
	public function del($id){

		$map = array();
		$map['id'] = $id;
		$result_del = $this->miactivity_channel_db->where($map)->setField('deleted',1);
		$result = array();
		if ($result_del) {
			$result['code'] = 1;
		} else {
			$result['code'] = 0;
			$error = $this->miactivity_channel_db->getError();
			$result['info'] = $error?$error:"发生未知错误";
		}
		Output::instance()->object($result);
	}
}